GBML manual / Core

Core basics

The always-available calls that belong to no other category - a running frame counter, pausing the game, writing a note to the log, starting the game over, two ways to run an event by hand, and the frame profiler.

What is in Core

Most of GBML's calls belong to a topic: drawing, motion, sound, rooms. Core holds the ones that belong to none of them, because they are about the game loop itself. Seven are covered below and fall into five jobs. The profile_ calls, which measure where each frame's time goes, have their own page: The frame profiler.

JobCalls
Telling timegame_frame
Freezing and resuming the gamegame_pause, game_paused
Looking inside the game while you test itlog_write
Starting the whole game overgame_restart
Running an event yourselfevent_call

Time is counted in frames

A game is a picture redrawn again and again. Each redraw is one frame, and the GBA makes about 60 of them every second. Everything you write in a Step event runs once per frame, in the fixed order set out in The order of a frame. That is why speeds in GBML are "pixels per frame" and delays are "frames": the frame is the machine's own clock tick.

game_frame is that clock. It is a whole number that starts at 0 when the game starts and goes up by one every frame, for the whole game. It belongs to no instance and nothing resets it. Two facts make it dependable as a clock:

Whole seconds come from dividing by 60. Both sides are whole numbers, so the fraction is dropped and you get a count of complete seconds (see Numbers for why):

// Step event
var seconds = game_frame() div 60;          // whole seconds since the game started
var blink_on = (game_frame() mod 60) < 30;  // on for half a second, off for half a second

game_frame is not the same thing as an instance's own frame, which is which picture of its sprite animation is showing, and it is not an alarm, which counts down for one instance. If you need "do this in 30 frames", an alarm is the tool. If you need "what time is it, for everything", it is game_frame. wave reads it for you.

Pausing

game_pause(true) freezes the game and game_pause(false) resumes it. game_paused() answers with yes or no. The engine does not pause on its own and reserves no button for it. A pause is something your project builds.

While the game is paused, these stop running: Step Begin, Alarms, Button events, Step, movement and Step End. Nothing moves and no timer ticks.

These keep running:

Sprite animation and tile animation stop too, because Project Settings has a box, Stop animations on pause, and it is ticked in every project. A sprite that is playing through its pictures holds the one it is showing, and the water and torches of a tileset hold theirs, until you call game_pause(false); then they carry on from where they stopped. Untick the box and they keep playing behind your menu. Project settings has the whole story, including what it means for a pause menu that wants its own sprites to move.

There is one consequence that surprises people. Button events do not run while paused. A Button Pressed event can pause the game, but it cannot resume it, because it is asleep by then. A pause menu reads its input with btn_pressed inside Draw GUI, the one event still awake:

// Step event: open the menu
if (btn_pressed(BTN_START)) { game_pause(true); }
// Draw GUI event: the menu, while it is up
if (game_paused()) {
    draw_text(4, 4, "PAUSED");
    if (btn_pressed(BTN_START)) { game_pause(false); }
}

That snippet leaves out one detail a real menu needs. btn_pressed stays true for the whole frame the press happens on, so the frame that pauses would also unpause. The pause_menu example project shows how to guard against it.

A room transition and a cutscene freeze the same things while they play, for the same reason.

Running an event by hand

Events normally run because something happened. Two calls let your code run one on purpose.

event_call runs a User event straight away. A User event is an event that nothing triggers by itself, so it runs only when a call names it. There are sixteen, user0 to user15. It comes in three forms, told apart by what stands before the dot:

// Step event
var target = instance_nearest(x, y, obj_enemy);
event_call(user0);            // this instance's own User 0
event_call(obj_enemy.user0);  // every instance of obj_enemy runs its User 0
event_call(target.user0);     // only the one instance the variable target points at

The first is the way to give a chunk of logic a name and call it from more than one place. The second broadcasts to a whole kind of object. The third reaches one instance you are holding a reference to, and does nothing if that instance has since been destroyed.

event_call(parent) is for objects that have a parent, a more general object they inherit from. A child that writes an event replaces its parent's version of it. Call event_call(parent) on the line where you want the parent's version to run as well, and your own code carries on after it. It always runs the same event you are inside, takes no arguments, and does nothing, with no error, when there is no parent version to run. It works directly inside an object's own event, and not inside a script. Objects and parents explains parents from the start.

Looking inside the game while you test it

A game that does something wrong gives you very little to go on. It just does the wrong thing. log_write lets the game tell you what it is doing. It writes one line of text to the emulator's log, a list of messages the player never sees. Put a value in the text with $"...":

// Step event
if (btn_pressed(BTN_A)) {
    log_write($"A pressed on frame {game_frame()}, x is {x}");
}

You read the lines in the mGBA emulator: Tools, then View logs.... A --release build writes nothing, and a real console has no log, so log_write is only for testing. The log_write page says where to look, what a line costs, and why it takes one text instead of a list of values.

Starting the whole game over

game_restart starts the game from the beginning, as if the console had just been switched on. It is the big sibling of room_restart, which reloads only the current room.

// Step event of the object that shows the game over screen
if (btn_pressed(BTN_START)) {
    game_restart();
}

Two facts matter. Your saved game is kept: whatever you wrote with save_commit is still there afterwards. Everything else is thrown away - every instance, every variable, the room, a pause - and it happens at once, so the lines after game_restart() never run. The game_restart page has the whole list.

The profile_ calls, which measure where each frame's time goes, have a page of their own: The frame profiler. The events themselves are in Events. Frame order is the full sequence a frame runs in, and shows exactly which steps game_pause skips.

See also: The order of a frameThe frame profilergame_framegame_pauselog_writegame_restartevent_callEvents